package com.kgcx.system.controller;

import com.kgcx.common.bean.RequestData;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.dbutils.domain.Dict;
import com.kgcx.dbutils.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 吴刚林
 * @date 2017/11/24
 */
@Api(description = "字典接口", tags = "字典接口")
@RestController
@RequestMapping("dict")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "查询字典", httpMethod = "GET", notes = "这是用来查询字典的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "字典ID", required = true, paramType = "query", dataType = "String", dataTypeClass = Long.class),
    })
    @GetMapping("getDict")
    public ReturnData<Dict> getDict() {
        RequestData requestData = this.getRequestData();
        Dict dict = dictService.getDict(requestData.getLong("kid"));
        return new ReturnData<>(dict);
    }

    @ApiOperation(value = "查询字典", httpMethod = "GET", notes = "这是用来查询字典的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("getDictList")
    public ReturnData<List<Dict>> getDictList() {
        RequestData requestData = this.getRequestData();
        List<Dict> dictList = dictService.getDictByType(requestData.getString("type"));
        return new ReturnData<>(dictList);
    }

    @ApiOperation(value = "新增字典", httpMethod = "POST", notes = "这是用来新增字典的接口")
    @PostMapping("addDict")
    public ReturnData addDict(@RequestBody Dict dict) {
        dictService.addDict(dict);
        return new ReturnData();
    }

    @ApiOperation(value = "修改字典", httpMethod = "POST", notes = "这是用来修改字典的接口")
    @PostMapping("updateDict")
    public ReturnData updateDict(@RequestBody Dict dict) {
        dictService.updateDict(dict);
        return new ReturnData();
    }

    @ApiOperation(value = "删除字典", httpMethod = "POST", notes = "这是用来删除字典的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "用户ID", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("removeDict")
    public ReturnData removeDict(@RequestParam Long kid) {
        dictService.removeDict(kid);
        return new ReturnData();
    }
}
